package com.bibrApp.difu.actvity.notice_center_mvp;

import android.content.Context;

import com.bibrApp.difu.Response.NoticeResBody;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.EncryptUtils;
import com.google.gson.Gson;

import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class NoticeCenterPresenter extends BasePresenter<INoticeCenterView> {
    NoticeCenterPresenter(Context context, INoticeCenterView iView) {
        super(context, iView);
    }

    public void getNoticeCenter(boolean isRefresh, int pageNum, int pageSize) {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("platform", "2");
        params.put("typeId", "1");
        params.put("pageSize", pageSize);
        params.put("pageNumber", isRefresh ? 1 : pageNum);
        RetrofitNet.getInstance().getApi()
                .getNoticeCenter(RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(params)))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            NoticeResBody noticeResBody = new Gson().fromJson(jsonStr, NoticeResBody.class);
                            iView.successOfNotice(noticeResBody,isRefresh);
                        }
                    }
                });
    }
}
